<?php
class ItemController extends Controller
{
    public $layout = 'admin';
    private $_model;

    public function  filters() {
        return array(
            'accessControl'
        );
    }

    public function accessRules()
    {
        return array(
            array('allow',
                'users' => array('@'),
            ),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
        );
    }
    
    public function actionIndex()
    {
        $model =  NItem::model();
        if ($this->getQuery('NItem')) {
            $model->attributes = $this->getQuery('NItem');
        }
        $this->display(array(
            'model' => $model,
            'dataProvider' => $model->search(),
        ));
    }

    public function actionCreate()
    {
        $model = new NItem();
        if($this->getPost('NItem')) {
            $model->attributes = $this->getPost('NItem');
            if ($model->save()) {
                $this->redirect(array('index'));
            }
        }

        $this->display(array(
            'model' => $model
        ));
    }

    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);
        if($this->getPost('NItem')) {
            $model->attributes = $this->getPost('NItem');
            if ($model->save()) {
                $this->redirect(array('index'));
            }
        }

        $this->display(array(
            'model' => $model
        ));
    }

    public function actionDelete($id)
    {
        if (Yii::app()->request->isPostRequest){
            NItem::model()->deleteByPk($id);
        }
    }

    protected function loadModel($id)
    {
        if (null === $this->_model) {
            $this->_model = NItem::model()->findByPk($id);
        }
        if (null === $this->_model)
            throw new CHttpException(404);
        return $this->_model;
    }
}